package exam.axhu.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import exam.axhu.entity.ShortAnswer;
import exam.axhu.service.ShortAnswerService;

@Service @Transactional
public class ShortAnswerServiceImpl implements ShortAnswerService{
//defaltBean: shortAnswerServiceImpl
	@Resource SessionFactory factory;
	
	public int save(ShortAnswer shortAnswer) {
		factory.getCurrentSession().persist(shortAnswer);
		return 0;	 //成功保存用户
	}
	
	public void delete(int... shortAnswerIDs) {
		for(int shortAnswerID : shortAnswerIDs){
			if(this.findByID(shortAnswerID)!=null)
				factory.getCurrentSession().delete(factory.getCurrentSession().load(ShortAnswer.class,shortAnswerID));
		}
		
	}

	public void update(ShortAnswer shortAnswer) {
		factory.getCurrentSession().merge(shortAnswer);
		
	}
	
	
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public ShortAnswer findByID(int shortAnswerID) {
		return (ShortAnswer) factory.getCurrentSession().get(ShortAnswer.class,shortAnswerID);
        
	}
	
	@SuppressWarnings("unchecked")
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<ShortAnswer> list() {
		return factory.getCurrentSession().createQuery("from ShortAnswer").list();
	}
	

}
